package com.fram.controller.admin;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.fram.bean.admin.ordermain.request.OrderDeliveredRequest;
import com.fram.bean.admin.ordermain.request.OrderRequest;
import com.fram.bean.sys.PageBean;
import com.fram.bean.sys.ResponseMsg;
import com.fram.common.canstant.ErrCode;
import com.fram.common.exception.ProAffairException;
import com.fram.common.exception.ProException;
import com.fram.common.util.PublicMethod;
import com.fram.common.util.apiversion.ApiVersion;
import com.fram.common.util.base.BaseAdminController;
import com.fram.entity.OrderRefund;
import com.fram.service.admin.OrderService;

/**后台-订单管理**/
@RestController
@RequestMapping("/admin/{version}/auth/order")
public class OrdersController extends BaseAdminController {

	/** 日志 **/
	private static Logger LOG = LoggerFactory.getLogger(OrdersController.class);

	@Autowired
	private OrderService orderService;

	/**
	 * 订单列表
	 * @param request
	 * @return
	 */
	@ApiVersion(1.0)
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	private ResponseMsg list(@RequestBody PageBean<OrderRequest> request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			if (request.getParam() == null) {
				request.setParam(new OrderRequest());
			}
			request.getParam().setDistributorId(getDistributorId());
			msg.setData(orderService.selectOrder(request));
		} catch (ProException e) {
			LOG.error("-后台订单-订单列表->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("--后台订单-订单列表-->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/**
	 * 订单详情展示
	 * @param id
	 * @return
	 */
	@ApiVersion(1.0)
	@RequestMapping(value = "/details", method = RequestMethod.GET)
	private ResponseMsg details(Long id) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(orderService.adminOrderDetail(id));
		} catch (ProException e) {
			LOG.error("-后台-订单详情->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("--后台-订单详情-->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/**
	 * 
	 * 订单改为已发货
	 */
	@ApiVersion(1.0)
	@RequestMapping(value = "/orderDelivered", method = RequestMethod.POST)
	private ResponseMsg update(@RequestBody OrderDeliveredRequest request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			orderService.orderDelivered(request);
		} catch (ProException e) {
			LOG.error("-后台-订单改为已发货 ->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("--后台-订单改为已发货-->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/**
	 * 审核失败修改退款单
	 */
	@ApiVersion(1.0)
	@RequestMapping(value = "/refund/apply/fail/update", method = RequestMethod.POST)
	private ResponseMsg updateApplyFailRefund(@RequestBody OrderRefund refund) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			orderService.updateApplyFailRefund(refund);
		} catch (ProAffairException e) {
			LOG.error("-后台-订单审核失败修改退款单 ->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("--后台-订单审核失败修改退款单-->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/**
	 * 申请退货成功
	 */
	@ApiVersion(1.0)
	@RequestMapping(value = "/refund/apply/success/update", method = RequestMethod.POST)
	private ResponseMsg updateApplySuccessRefund(@RequestBody OrderRefund refund) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setReturnMsg(orderService.updateApplySuccessRefund(refund, getDistributorId()));
		} catch (ProAffairException e) {
			LOG.error("-后台-订单申请退货成功 ->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("--后台-订单申请退货成功-->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/**
	 * 收到退货快递过后执行退款
	 */
	@ApiVersion(1.0)
	@RequestMapping(value = "/refund/price", method = RequestMethod.POST)
	private ResponseMsg refundUserPrice(@RequestBody OrderRefund refund) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			orderService.refundUserPrice(refund, getDistributorId());
		} catch (ProAffairException e) {
			LOG.error("-后台-订单收到退货快递过后执行退款 ->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("--后台-订单收到退货快递过后执行退款-->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
	// PC 端 首页展示 
	@ApiVersion(1.0)
	@RequestMapping(value = "/page/report/form")
	private ResponseMsg reportform() {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(orderService.reportForm(getDistributorId()));
		} catch (ProAffairException e) {
			LOG.error("-后台-订单数据报表 ->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-后台-订单据报表款-->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
}
